import CommonAction from '../../../../../public/actions/employing/common';


import {
    output,
    print,
    toast
} from 'nc-lightapp-front';
import {saveValidate} from "src/hrtrn/public/functions/unit";
import FormEdit from './formEdit';
import {getAppPageConfig} from 'src/hrpub/common/utils/utils';
export default class Main extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
    }

    extend = [FormEdit]

    // 删除一个申请单据
    deleteBill = (selectedRows) => {
        this.comp.action.rowAct.deleteRows(selectedRows)
            .then((res) => {
                if(res) {
                    this.goToBackMainPage();
                }
            });
    }
    
    // 提交单据
    submitBill = async (selectedRows) => {
        const {action} = this.comp;

        await action.rowAct.submitOnes(selectedRows);
        action.formAct.toBrowsePage(selectedRows);
    }

    // 收回单据
    callbackBill = async (selectedRows) => {
        const {action} = this.comp;

        await action.rowAct.callbackOnes(selectedRows);
        action.formAct.toBrowsePage(selectedRows);
    }
    // 执行
    excuteBill = async(selectedRows) => {
        const {action} = this.comp;

        await action.rowAct.excuteOnes(selectedRows);
    }

    didAllInstance = () => {
        this.setButtonHandleMap();
    }
    
    // 设置按钮映射
    setButtonHandleMap = () => {
        const {action} = this.comp;
        console.log(this.comp);

        this.buttonHandleMap = {
            'save': this.saveAddPage,
            'browse_status': {
                'edit': this.toAddPageEditStatus,
                'submit': this.submitBill,
                'callback': this.callbackBill,
                'delete': this.deleteBill,
                'excute': this.excuteBill,
                'copy':  action.rowAct.copyOnes,
                'print': this.doPrint,
                'output': this.doOutput,
                'CheckBudget': action.orgSta.openOrgSituationModal,
                'cardrpt': this.jointSearch,
                'viewctrt': action.vcAct.showViewContactModal,
                'add': action.formAct.toAddPage,
                'refresh': action.formAct.toBrowsePage,
                'file': this.comp.props.fileManager
            }
        };
    }

    // 新增页面头部按钮点击处理
    addHeaderClick = (btnCode) => {
        const {action, props} = this.comp;
        const {form} = props;
        return (props, bCode) => {
            console.log(bCode);
            let selectedRows = [{
                ...form.getAllFormValue('card').rows[0]
            }];
            
            if(typeof this.buttonHandleMap[btnCode] === 'function') {
                this.buttonHandleMap[btnCode](selectedRows);
            }
            else if(typeof this.buttonHandleMap[btnCode][bCode] === 'function') {
                this.buttonHandleMap[btnCode][bCode](selectedRows);
            }
        }
    }

    // 从浏览态变成编辑态
    toAddPageEditStatus = (selectedRows) => {
        this.comp.action.rowAct.editOnes(selectedRows);
    }

    // 新增页面保存
    saveAddPage = async () => {
        const {props, action} = this.comp;
        const {dispatch, form, emp} = props;

        if(form.isCheckNow('card')) {
            let postData = {
                model: form.getAllFormValue('card'),
                pk_org: emp.orgValue.refpk
            };
            let bodys_code = ['psninfo','oldinfo','newinfo','neworgs','contorgs','execinfo','tailinfo','auditinfo'];
            saveValidate(props, emp.pageid, 'card', bodys_code, 'extcard').then(async ()=>{
                try {
                    let res = await dispatch({
                        type: 'emp/checkAddPageSave',
                        payload: {
                            postData: postData
                        }
                    });

                if(res.data && res.data.confirmMsg) {
                    this.promptBox({
                        color: 'warning',
                        title: emp.language['gx6008-000018'], // 提示
                        content: res.data.confirmMsg,
                        beSureBtnClick: async () => {
                            let res = await dispatch({
                                type: 'emp/addPageSave',
                                payload: {
                                    postData: postData
                                }
                            });

                            if(res.success) {
                                this.toast({
                                    color: 'success',
                                    content: emp.language['gx6008-000030'] // 保存成功
                                });
                                action.formAct.toBrowseStatus(res.data.head.card);
                            }
                        }
                    });
                }
                else {
                    let res = await dispatch({
                        type: 'emp/addPageSave',
                        payload: {
                            postData: postData
                        }
                    });

                        if(res.success) {
                            this.toast({
                                color: 'success',
                                content: emp.language['gx6008-000030'] // 保存成功
                            });
                            action.formAct.toBrowseStatus(res.data.head.card);
                        }
                    }
                }
                catch(e) {
                }
            })
        }
    }

    // 返回按钮返回主页面表格
    goToBackMainPage = () => {
        const {props} = this.comp;
        const {dispatch, button,emp} = props;

        this.update({
            page: 'main'
        });
        if(emp.widget){
            button.setButtonVisible({
                add: false,
                edit: false,
                delete: false,
                query: false,
                submit: false,
                print: false,
                batchadd: false,
                callback: false,
                approveinfo: false,
                refresh: false,
                save: false,
                cancel: false,
                first: false,
                pre: false,
                next: false,
                last: false,
                copy: false,
                assfunc: false,
                cardrpt: false,
                file: false
            });
            this.pubSub.publish('getWidgetTableData');
        }else{
            button.setButtonVisible({
                add: true,
                edit: false,
                delete: true,
                query: true,
                submit: true,
                print: true,
                batchadd: true,
                callback: true,
                approveinfo: true,
                refresh: true,
                save: false,
                cancel: false,
                first: false,
                pre: false,
                next: false,
                last: false,
                copy: false,
                assfunc: true,
                cardrpt: false,
                file: false,
                excute: true    
            });
            this.pubSub.publish('getMainTableData');
        }
        // button.setButtonVisible({
        //     save: false,
        //     cancel: false
        // });
    }

    // 取消编辑
    cancelFormEdit = () => {
        const {props, action} = this.comp;
        const {emp, dispatch, form, meta, button} = props;

        this.promptBox({
            color: 'info',
            title: emp.language['gx6008-000036'], // '确认取消',
            content: emp.language['gx6008-000037'], // '是否确认取消
            beSureBtnClick: () => {
                let formValue = form.getAllFormValue('card');
                let billId = formValue.rows[0].values['pk_hi_stapply'].value;

                if(emp.page === 'add') {
                    this.cancelPost();
                }
                if(emp.page === 'add' || !billId) {
                    this.goToBackMainPage();
                }
                else {
                    action.formAct.toBrowseStatus();
                    this.showHistoryData();
                }
            }
        });
    }

    // 新增取消需要请求一个接口
    cancelPost = () => {
        const {props, action} = this.comp;
        const {emp, dispatch, form, meta, button} = props;

        let postData = {
            model: form.getAllFormValue('card'),
            pk_org: emp.orgValue.refpk
        };

        return dispatch({
            type: 'emp/cancelAdd',
            payload: {
                postData: postData
            }
        });
    }

    // 根据存储的数据展示编辑前
    showHistoryData = () => {
        const {props} = this.comp;
        const {emp, dispatch, form} = props;

        if(emp.addPageHistoryData) {
            form.setAllFormValue({
                card: emp.addPageHistoryData
            });
            this.update({
                addPageHistoryData: null 
            });
        }
    }

    // 打印
    doPrint = () => {
        const {props} = this.comp;
        const {emp} = props;
        let cardFormValue = this.comp.props.form.getAllFormValue('card');
        print('pdf', '/nccloud/hrtrn/dimissionmng/DimissionPrintAction.do', {
            appcode: getAppPageConfig().appcode,
            nodekey: emp.fromApprove?getAppPageConfig().appcode:'60081060', //'60081060',
            oids: [cardFormValue.rows[0].values['pk_hi_stapply'].value]
        });
    }

    // 输出
    doOutput = () => {
        const {
            props: {
                emp
            }
        } = this.comp;
        let cardFormValue = this.comp.props.form.getAllFormValue('card');

        output({
            url: '/nccloud/hrtrn/dimissionmng/DimissionPrintAction.do',
            data: {
                appcode: '60092060',
                nodekey: '60081060',
                oids: [cardFormValue.rows[0].values['pk_hi_stapply'].value],
                outputType: 'output',
                filename: emp.language['gx6008-000199'] // 离职办理
            },
            callback: () => {
            }
        });
    }

    // 联查人员卡片
    jointSearch = async () => {
        const {props} = this.comp;
        const {form, btnAct} = props;
        
        let formData = form.getAllFormValue('card');
        
        btnAct.wpsview.openView(formData.rows);
    }
}